<?php
class NavModel extends Model {
	protected $_validate = array(
	    array('title','require','请填写标题！'),
    );
	protected $_auto = array(
        array('datetime','time',1,'function'),
		array('datetime','time',2,'function'),
    );
	public function getListToArray($pid=0){
		$re=$this->where('pid='.$pid)->order('sort')->select();
		if($re!==false){
			foreach($re as $k=>$v){
				$re[$k]['sub']=$this->getListToArray($v['id']);
			}
			return $re;
		}
	}
	
	public function getListToArray1($pid=0,$siteid=0,$deep=0,$arr=array()){
		$re=$this->where(array('pid'=>$pid,'site_id'=>$siteid))->order('sort')->select();
		if($re!==false){
			$deep++;
			foreach($re as $k=>$v){
				$v['deep']=$deep;
				$arr[]=$v;
				$arr=$this->getListToArray1($v['id'],$siteid,$deep,$arr);
			}
			return $arr;
		}
	}
	
	//复制模块导航
	public function copy($siteid, $tositeid,$map, $pid = 0, $modules_id = 0){
		
		$re=$this->where($map)->order('sort,id')->select();
		if($re!==false){
			foreach($re as $k=>$v){
				$map['pid'] = $v['id'];
				$v['site_id'] = $tositeid;
				$v['modules_id'] = $modules_id;
				unset($v['id']);
				$v['pid'] = $pid;
				$pid1 = $this->data($v)->add();
				//echo '写入一个导航<br/>';
				
				//ob_end_flush();
				
				$this->copy($siteid, $tositeid,$map, $pid1, $modules_id);
			}
			return true;
		}
	}//复制模块导航
	public function copyself($siteid, $tositeid,$map, $pid = 0, $modules_id = 0){
		
		$re=$this->where($map)->order('sort,id')->select();
		if($re!==false){
			foreach($re as $k=>$v){
				$map['pid'] = $v['id'];
				$v['site_id'] = $tositeid;
				$v['modules_id'] = $modules_id;
				unset($v['id']);
				$v['pid'] = $pid;
				$pid1 = $this->data($v)->add();
				$this->copyself($siteid, $tositeid,$map, $pid1, $modules_id);
			}
			return true;
		}
	}
}

?>